package com.free.front.bf.product;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.free.common.vo.ResultModel;
import com.free.ext.aliababa.AlibabaSdkOcr;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@Component
public class ProductVinAlibabaBf {


    public ResultModel getByVinFile(MultipartFile file){
        Map<String,String> resMap = new HashMap<>();
        ResultModel res =  drivinglicense(file);
        if(res.isSuccess()){
            resMap.put("data", res.getData().toString());
            return ResultModel.success(resMap);
        }
        res = identificationVinCode(file);
        if(res.isSuccess()){
            resMap.put("data", res.getData().toString());
            return ResultModel.success(resMap);
        }
        return ResultModel.failure(null,"图片解析失败");
    }

    //图片识别vin码
    public ResultModel identificationVinCode(MultipartFile file){
        try {
            //读取配置文件
            Properties p = new Properties();
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("setting-web.properties");
            p.load(inputStream);
            //请根据线上文档修改configure字段
            JSONObject configObj = new JSONObject();
            configObj.put("multi_crop", p.getProperty("vin.multi_crop"));
            String config_str = configObj.toString();
            configObj.put("min_size", p.getProperty("vin.min_size"));
            //拼装请求body的json字符串
            String bodys = AlibabaSdkOcr.getJosnString(file.getBytes(),config_str).toString();
            String host = p.getProperty("vin.alibabaHost");
            String path = p.getProperty("vin.alibabaPath");
            String appcode = p.getProperty("vin.alibabaAppCode");
            HttpResponse httpResponse = AlibabaSdkOcr.postOperation(host,path,appcode,bodys);
            if(httpResponse == null){
                return ResultModel.failure(false,"图片解析失败！");
            }
            String res = EntityUtils.toString(httpResponse.getEntity());
            JSONObject res_obj = JSON.parseObject(res);
            return ResultModel.success(res_obj.getString("vin"));
        } catch (Exception e) {
            return ResultModel.failure(false,"图片解析失败！");
        }
    }

    //行驶证的解析
    public ResultModel drivinglicense(MultipartFile file){
        try {
            //读取配置文件
            Properties p = new Properties();
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("setting-web.properties");
            p.load(inputStream);
            //请根据线上文档修改configure字段
            JSONObject configObj = new JSONObject();
            configObj.put("side", "face");  //正反面类型face/back
            String config_str = configObj.toString();
            configObj.put("min_size", p.getProperty("driv.min_size"));
            // 对图像进行base64编码
            String bodys = "";
            bodys = AlibabaSdkOcr.getJosnString(file.getBytes(),config_str).toString();
            String host = p.getProperty("driv.alibabaHost");
            String path = p.getProperty("driv.alibabaPath");
            String appcode = p.getProperty("driv.alibabaAppCode");
            HttpResponse httpResponse = AlibabaSdkOcr.postOperation(host,path,appcode,bodys);
            if(httpResponse == null){
                return ResultModel.failure(false,"图片解析失败！");
            }
            String res = EntityUtils.toString(httpResponse.getEntity());
            JSONObject res_obj = JSON.parseObject(res);
            if(!res_obj.containsKey("vin")){
                return ResultModel.failure();
            }
            return ResultModel.success(res_obj.get("vin"));
        } catch (Exception e) {
            return ResultModel.failure(false,"图片解析失败！");
        }
    }
}
